package com.debao.web.controller.debao;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.debao.common.core.domain.entity.SysUser;
import com.debao.framework.web.service.SysRegisterService;
import com.debao.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.debao.common.annotation.Log;
import com.debao.common.core.controller.BaseController;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.enums.BusinessType;
import com.debao.system.domain.UserCard;
import com.debao.system.service.IUserCardService;
import com.debao.common.utils.poi.ExcelUtil;
import com.debao.common.core.page.TableDataInfo;

/**
 * 会员账户Controller
 * 
 * @author DeBao
 * @date 2024-08-14
 */
@RestController
@RequestMapping("/debao/userCard")
public class UserCardController extends BaseController
{
    @Autowired
    private IUserCardService userCardService;

    @Resource
    private SysRegisterService registerService;
    @Resource
    private ISysUserService userService;

    /**
     * 查询会员账户列表
     */
    @PreAuthorize("@ss.hasPermi('debao:userCard:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserCard userCard)
    {
        startPage();
        List<UserCard> list = userCardService.selectUserCardList(userCard);
        return getDataTable(list);
    }

    /**
     * 导出会员账户列表
     */
    @PreAuthorize("@ss.hasPermi('debao:userCard:export')")
    @Log(title = "会员账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserCard userCard)
    {
        List<UserCard> list = userCardService.selectUserCardList(userCard);
        ExcelUtil<UserCard> util = new ExcelUtil<UserCard>(UserCard.class);
        util.exportExcel(response, list, "会员账户数据");
    }

    /**
     * 获取会员账户详细信息
     */
    @PreAuthorize("@ss.hasPermi('debao:userCard:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userCardService.selectUserCardById(id));
    }

    /**
     * 新增会员账户
     */
    @PreAuthorize("@ss.hasPermi('debao:userCard:add')")
    @Log(title = "会员账户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserCard userCard)
    {
        // 获取用户是否已存在【手机号是否被注册】
        boolean isExist = userService.isExistUser(userCard.getPhone());
        // 用户不存在
        if (!isExist) {
            // 开始注册用户 | 注册成功返回
            userCard.setUserId(registerService.register(userCard.getUserName(), userCard.getPhone()));
            return toAjax(userCardService.insertUserCard(userCard));
        } else {
            // 用户已存在
            SysUser user = userService.getUserByPhone(userCard.getPhone());
            if (user != null) {
                userCard.setUserId(user.getUserId());
                return toAjax(userCardService.insertUserCard(userCard));
            }
        }
        return AjaxResult.error("会员开通失败！");
    }

    /**
     * 修改会员账户
     */
    @PreAuthorize("@ss.hasPermi('debao:userCard:edit')")
    @Log(title = "会员账户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCard userCard)
    {
        // 获取用户是否已存在【手机号是否被注册】
        boolean isExist = userService.isExistUser(userCard.getPhone());
        // 用户存在
        if (isExist) {
            return toAjax(userCardService.updateUserCard(userCard));
        }
        return AjaxResult.error("您输入的手机号还未注册，请先添加会员账户");
    }

    /**
     * 删除会员账户
     */
    @PreAuthorize("@ss.hasPermi('debao:userCard:remove')")
    @Log(title = "会员账户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userCardService.deleteUserCardByIds(ids));
    }
}
